
package fileby.ctrl.transferhandlers.impl;

import fileby.model.IFavoriteManager;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.TransferHandler;

/**
 *
 * @author Oscar
 */
class FavoriteTransferHandler extends TransferHandler {

    private IFavoriteManager favoriteManager;

    public FavoriteTransferHandler(IFavoriteManager favoriteManager) {
        this.favoriteManager = favoriteManager;
    }

    /*
     * This method indicates whether a component would accept an import
     * of the given set of data flavors prior to actually attempting to
     * import it. In this case we only support javaFileListFlavor.
     */
    @Override
    public boolean canImport(TransferHandler.TransferSupport support) {
        return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
    }

    /*
     *  Causes a transfer with files to a component from the DnD action.
     */
    @Override
    public boolean importData(TransferHandler.TransferSupport support) {
        Transferable t = support.getTransferable();
        try {
            List<File> fileList = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
            favoriteManager.addFavorites(fileList);
        } catch (UnsupportedFlavorException e) {
            return false;
        } catch (IOException e) {
            return false;
        }
        return true;
    }

}
